#! /bin/sh

if [ ${#} -lt 1 ] ; then
	echo "Destination directory not specified."
	exit 1
fi

DEST_DIR=${1}

if [ -z ${MKDIR_P} ] ; then
	MKDIR_P="mkdir -p"
fi

${MKDIR_P} ${DEST_DIR}
cd "${DEST_DIR}"

COUNTRY="XX"
STATE="State"
LOCATION="Location"
ORGANIZATION="Organization"
ORGANIZATION_UNIT="Organization Unit"
COMMON_NAME="example.codestation.org"
EMAIL_ADDRESS="admin@example.codestation.org"

SUBJECT=""
SUBJECT="${SUBJECT}/C=${COUNTRY}"
SUBJECT="${SUBJECT}/ST=${STATE}"
SUBJECT="${SUBJECT}/L=${LOCATION}"
SUBJECT="${SUBJECT}/O=${ORGANIZATION}"
SUBJECT="${SUBJECT}/OU=${ORGANIZATION_UNIT}"
SUBJECT="${SUBJECT}/CN=${COMMON_NAME}"
SUBJECT="${SUBJECT}/emailAddress=${EMAIL_ADDRESS}/"

set -e

openssl genpkey -algorithm rsa -pkeyopt rsa_keygen_bits:2048 -out key 2> /dev/null
openssl req -x509 -new -key key -out pem -days 3652 -subj "${SUBJECT}"

